STK.register('PhotoApp',function(){

    var Backbone = STK.backbone,
        $ = STK.Q,
        Slide = STK.slide;

    var PhotoApp = {
        init:function(){

            var workplace = Backbone.Router.extend({
                routes:{
                    "index":"initAlbums",
                    "":"initAlbums",
                    "albums":"initAlbums",
                    "photos/:album":"initPhotos",
					"photos":"initPhotos",
                    "slide/:name/:key":"initSlide",
                    "slide":"initSlide"
                }
            });

            var routes = new workplace();
			var container = $('#J_InitWrap')[0];
            routes.on('route:initAlbums',function(){
				var ul = $('#J_InitWrap').find('ul');
				if (ul) {
					ul.fadeOut('slow', function(){
						container.innerHTML = '';
						STK.albums.init( container );
					}).end().fadeIn('slow');
				}
            });
			
            routes.on('route:initPhotos',function(album){
				var ul = $('#J_InitWrap').addClass('album').find('ul');
				if (ul) {
					if (album) {
						album = decodeURIComponent(album);
						var topCenter = $('.top_center'), txtCenter = $('.txt_center');
						if (topCenter && txtCenter) {
							var span = txtCenter.find('.album_title');
							if (span) {
								span.text(album);
							}
							topCenter.hide('fast', function(){
								txtCenter.show('fast', function(){
									var a = txtCenter.find('a');
									if (a) {
										a.unbind('click').bind('click', function(){
											txtCenter.hide();
											topCenter.show();
											location.hash = '';
										});
									}
								});
							});
						}
					}
					
					ul.fadeOut('slow', function(){
						container.innerHTML = '';
						STK.photoList.init( container );
						
						var data;
						if(!album) {
							data = STK.photoList.getData();
							STK.photoList.render(data);
						} else {
							//album = decodeURIComponent(album);
							data = STK.photoList.getPhotos(album) || {};
							STK.photoList.render(album, data);
						}
					}).end().fadeIn('slow');
				}
            });
            var myslide = new Slide();

            routes.on('route:initSlide',function(name,key){
                if(key != myslide.key){
					var container = $('#J_InitWrap');
					container.fadeOut('slow', function(){
						container.innerHTML = '';
						myslide.render(name,key);
					}).end();
					container.fadeIn('slow');
                }
            });
			
			var tabs = $('.top_center').find('a');
			tabs.each(function(){
				$(this).unbind('click').click(function(){
					tabs.each(function(k, v){
						$(v).removeClass('current');
					});
					$(this).addClass('current');
					location.hash = $(this).hasClass('all') ? '#photos' : '';
				});
			});
			
            Backbone.history.start({slient:true});
        }
    };
    return PhotoApp;
});
